package com.mall.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.commons.exceotion.BusinessException;
import com.mall.commons.exceotion.ErrorResult;
import com.mall.dubbo.mappers.UmsResourceMapper;
import com.mall.dubbo.mappers.UmsRoleResourceRelationMapper;
import com.tanhua.model.domain.UmsResource;
import com.tanhua.model.domain.UmsRoleResourceRelation;
import com.tanhua.model.dto.ResourceListDao;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@DubboService
public class UmsResourceApiImpl implements UmsResourceApi {

    @Autowired
    private UmsResourceMapper umsResourceMapper;

    @Autowired
    private UmsRoleResourceRelationMapper umsRoleResourceRelationMapper;


    /**
     * 分页模糊查询后台资源
     *
     * @param resourceListDao
     * @return
     */
    @Override
    public Page listResource(ResourceListDao resourceListDao) {
        Page<UmsResource> page = new Page<>(resourceListDao.getPageNum(), resourceListDao.getPageSize());
        LambdaQueryWrapper<UmsResource> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(resourceListDao.getUrlKeyword()), UmsResource::getUrl, resourceListDao.getUrlKeyword())
                .like(StringUtils.isNotEmpty(resourceListDao.getNameKeyword()), UmsResource::getName, resourceListDao.getNameKeyword())
                .eq(null != resourceListDao.getCategoryId(), UmsResource::getCategoryId, resourceListDao.getCategoryId());
        return umsResourceMapper.selectPage(page, wrapper);
    }

    /**
     * 根据ID删除后台资源
     *
     * @param id
     * @return
     */
    @Override
    public boolean delete(Integer id) {
        LambdaQueryWrapper<UmsRoleResourceRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(UmsRoleResourceRelation::getResourceId, id);
        if (umsRoleResourceRelationMapper.selectCount(wrapper) > 0) {
            throw new BusinessException(ErrorResult.serveError());
        }
        return umsResourceMapper.deleteById(id) > 0 ? true : false;
    }

    /**
     * //修改后台资源
     *
     * @param id
     * @param umsResource
     * @return
     */
    @Override
    public boolean update(Integer id, UmsResource umsResource) {
        return umsResourceMapper.updateById(umsResource) > 0 ? true : false;
    }

    /**
     * 添加后台资源
     *
     * @param umsResource
     * @return
     */
    @Override
    public boolean create(UmsResource umsResource) {
        LambdaQueryWrapper<UmsResource> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsResource::getName, umsResource.getName());
        //如果名字重复直接返回false
        if (umsResourceMapper.selectCount(wrapper) > 0) {
            return false;
        }
        umsResource.setCreateTime(new Date(System.currentTimeMillis()));
        return umsResourceMapper.insert(umsResource) > 0 ? true : false;
    }

    /**
     * 查询所有后台资源
     *
     * @return
     */
    @Override
    public List<UmsResource> listAll() {
        return umsResourceMapper.selectList(new LambdaQueryWrapper<>());
    }
}
